package com.huawei.appgallery.updatemanager.ui.widget;

import android.app.Activity;
import android.app.Application;
import android.app.Application.ActivityLifecycleCallbacks;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.appgallery.foundation.account.bean.UserSession;
import com.huawei.appgallery.foundation.application.pkgmanage.model.update.ApkUpgradeInfo;
import com.huawei.appgallery.foundation.ui.framework.cardkit.bean.CardBean;
import com.huawei.appgallery.updatemanager.ui.cardkit.bean.UpdateRecordCardBean;
import com.huawei.appgallery.updatemanager.ui.cardkit.card.UpdateRecordCard;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant.KeyAndValue;
import com.huawei.secure.android.common.intent.SafeBroadcastReceiver;
import huawei.widget.HwButton;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import o.azb;
import o.ccv;
import o.cdk;
import o.cdz;
import o.ckm;
import o.czi;
import o.czk$b;
import o.czk$c;
import o.czk$e;
import o.czr;
import o.czu;
import o.czx;
import o.dao;
import o.dba;
import o.dbj;
import o.dbn;
import o.Logger;
import o.dob;
import o.epv;
import o.ActivityUtil;
import o.MinimumFactory;
import o.ll;

public class UpdateView extends LinearLayout implements czx, dbj {
    private View ʻ;
    private View ʼ;
    private View ʽ;
    private View ˊ;
    private boolean ˊॱ;
    private final View ˋ;
    private b ˋॱ;
    private Handler ˎ;
    private TextView ˏ;
    private List<UpdateRecordCard> ˏॱ;
    private boolean ͺ;
    private ImageView ॱ;
    private String ॱˊ;
    private HwButton ॱॱ;
    private BroadcastReceiver ॱᐝ;
    @NonNull
    private List<ApkUpgradeInfo> ᐝ;
    private BroadcastReceiver ᐝॱ;

    static class a implements OnClickListener {
        private a() {
        }

        public void onClick(View view) {
            cdk.ॱ(view.getContext(), "071206", "01|" + UserSession.getInstance().getUserId() + HiAnalyticsConstant.REPORT_VAL_SEPARATOR + cdz.ˎ());
            dao.ˏ().ˎ().ॱ(view.getContext());
        }
    }

    static class b implements ActivityLifecycleCallbacks {
        private UpdateView ॱ;

        private b(UpdateView updateView) {
            this.ॱ = updateView;
        }

        public void onActivityCreated(Activity activity, Bundle bundle) {
        }

        public void onActivityStarted(Activity activity) {
        }

        public void onActivityResumed(Activity activity) {
            if (this.ॱ != null && this.ॱ.getContext() == activity) {
                this.ॱ.ͺ = true;
                if (this.ॱ.ˊॱ) {
                    czi.ˎ.ˋ("UpdateView", "resumed refreshAllCard");
                    this.ॱ.ˏ();
                    this.ॱ.ˊॱ = false;
                }
            }
        }

        public void onActivityPaused(Activity activity) {
            if (this.ॱ != null && this.ॱ.getContext() == activity) {
                this.ॱ.ͺ = false;
            }
        }

        public void onActivityStopped(Activity activity) {
        }

        public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
        }

        public void onActivityDestroyed(Activity activity) {
        }
    }

    static class e implements OnClickListener {
        private e() {
        }

        public void onClick(View view) {
            cdk.ॱ(view.getContext(), "070906", KeyAndValue.NUMBER_01);
            dao.ˏ().ˎ().ॱ(view.getContext());
        }
    }

    private void ˏ() {
        for (UpdateRecordCard updateRecordCard : this.ˏॱ) {
            updateRecordCard.ˏ(updateRecordCard.ˌ());
        }
    }

    private void ˏ(String str, boolean z) {
        if (z) {
            this.ॱˊ = str;
        } else {
            this.ॱˊ = "";
        }
        for (UpdateRecordCard updateRecordCard : this.ˏॱ) {
            CardBean ˌ = updateRecordCard.ˌ();
            if (ˌ instanceof UpdateRecordCardBean) {
                ((UpdateRecordCardBean) ˌ).ॱ(ˌ.ॱـ().equals(this.ॱˊ));
            }
            updateRecordCard.ˏ(ˌ);
        }
    }

    public UpdateView(Context context) {
        this(context, null);
    }

    public UpdateView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public UpdateView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.ˏॱ = new ArrayList(3);
        this.ˋॱ = new b();
        this.ˊॱ = false;
        this.ॱᐝ = new SafeBroadcastReceiver(this) {
            final /* synthetic */ UpdateView ˊ;

            {
                this.ˊ = r1;
            }

            public void onReceiveMsg(Context context, Intent intent) {
                if (this.ˊ.ͺ) {
                    this.ˊ.ˏ();
                } else {
                    this.ˊ.ˊॱ = true;
                }
            }
        };
        this.ᐝॱ = new SafeBroadcastReceiver(this) {
            final /* synthetic */ UpdateView ˎ;

            {
                this.ˎ = r1;
            }

            public void onReceiveMsg(Context context, Intent intent) {
                this.ˎ.ˏ(intent.getStringExtra("card_packageName"), intent.getBooleanExtra("card_isExpand", false));
            }
        };
        this.ˎ = new Handler(Looper.getMainLooper());
        LayoutInflater.from(context).inflate(czk$e.ॱॱ, this);
        this.ˊ = findViewById(czk$c.ॱʽ);
        azb.ˏ(this.ˊ);
        this.ˏ = (TextView) findViewById(czk$c.ᐨ);
        this.ॱ = (ImageView) findViewById(czk$c.ᐝˋ);
        int ʻ = azb.ʻ(getContext()) - azb.ˊ(getContext());
        int ॱॱ = azb.ॱॱ(getContext()) - azb.ˏ(getContext());
        this.ʽ = findViewById(czk$c.ˋˊ);
        LayoutParams layoutParams = new LinearLayout.LayoutParams(-2, -2);
        layoutParams.topMargin = -context.getResources().getDimensionPixelSize(czk$b.ˋ);
        layoutParams.setMarginStart(ʻ);
        layoutParams.setMarginEnd(ॱॱ);
        this.ʽ.setLayoutParams(layoutParams);
        ˏ(this.ʽ);
        layoutParams = new LinearLayout.LayoutParams(-2, -2);
        layoutParams.setMarginStart(ʻ);
        layoutParams.setMarginEnd(ॱॱ);
        this.ʼ = findViewById(czk$c.ˊˊ);
        this.ʼ.setLayoutParams(layoutParams);
        ˏ(this.ʼ);
        this.ʻ = findViewById(czk$c.ˌ);
        this.ʻ.setLayoutParams(layoutParams);
        ˏ(this.ʻ);
        this.ॱॱ = (HwButton) findViewById(czk$c.ॱʼ);
        azb.ॱॱ(this.ॱॱ);
        this.ˋ = findViewById(czk$c.ᐝᐝ);
        this.ˊ.setOnClickListener(new a());
        this.ॱॱ.setOnClickListener(new e());
        this.ᐝ = ((czu) dbn.ˎ(czu.class)).ˎ(true, 1);
        ˊ(null);
        ॱ(((czu) dbn.ˎ(czu.class)).ʻ(true, 1) + this.ᐝ.size());
        this.ͺ = true;
        ˊ(false);
    }

    private void ˏ(@NonNull View view) {
        azb.ʼ(view.findViewById(czk$c.ـ), epv.ᐝ());
        azb.ʼ(view.findViewById(czk$c.ᐝ), epv.ᐝ());
    }

    private void ˊ(boolean z) {
        if (getContext() instanceof Activity) {
            try {
                Object invoke = dob.ˏ(Activity.class, "isResumed", new Class[0]).invoke(getContext(), new Object[0]);
                if (invoke instanceof Boolean) {
                    boolean z2 = this.ͺ;
                    this.ͺ = ((Boolean) invoke).booleanValue();
                    if (z && this.ͺ != z2) {
                        ˏ();
                    }
                    Logger.ˎ("UpdateView", "read isResume method:" + this.ͺ);
                }
            } catch (IllegalAccessException e) {
                Logger.ˋ("UpdateView", "can not reflect isResume IllegalAccessException");
            } catch (InvocationTargetException e2) {
                Logger.ˋ("UpdateView", "can not reflect isResume InvocationTargetException");
            }
        }
    }

    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        ˊ(true);
        ((czr) dbn.ˎ(czr.class)).ˎ(hashCode() + "UpdateView", this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("updatemanager.refresh.update.expand.action");
        ll.ˏ(getContext()).ˏ(this.ᐝॱ, intentFilter);
        ActivityUtil.registerReceiver(getContext(), new IntentFilter(ccv.ˋ()), this.ॱᐝ);
        ॱ();
    }

    private void ॱ() {
        Context context = getContext();
        if (context != null) {
            context = context.getApplicationContext();
            if (context instanceof Application) {
                ((Application) context).registerActivityLifecycleCallbacks(this.ˋॱ);
                return;
            } else {
                czi.ˎ.ˊ("UpdateView", "Attached not application");
                return;
            }
        }
        czi.ˎ.ˊ("UpdateView", "Attached context is null");
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        ((czr) dbn.ˎ(czr.class)).ˏ(hashCode() + "UpdateView");
        ll.ˏ(getContext()).ˋ(this.ᐝॱ);
        ActivityUtil.unregisterReceiver(getContext(), this.ॱᐝ);
        ˎ();
    }

    private void ˎ() {
        Context context = getContext();
        if (context != null) {
            context = context.getApplicationContext();
            if (context instanceof Application) {
                ((Application) context).unregisterActivityLifecycleCallbacks(this.ˋॱ);
                return;
            } else {
                czi.ˎ.ˊ("UpdateView", "Detached not application");
                return;
            }
        }
        czi.ˎ.ˊ("UpdateView", "Detached context is null");
    }

    public void ˏ(String str, ckm o_ckm) {
    }

    public void ˋ(final int i) {
        this.ˎ.post(new Runnable(this) {
            final /* synthetic */ UpdateView ˋ;

            public void run() {
                this.ˋ.ॱ(i);
                this.ˋ.ˊ(((czu) dbn.ˎ(czu.class)).ˎ(true, 1));
            }
        });
    }

    public void ˎ(int i) {
    }

    public void ˏ(String str) {
    }

    public void ˋ() {
        this.ˎ.post(new Runnable(this) {
            final /* synthetic */ UpdateView ˋ;

            {
                this.ˋ = r1;
            }

            public void run() {
                this.ˋ.ॱ(0);
                this.ˋ.ॱ.setVisibility(8);
                this.ˋ.ॱॱ.setVisibility(8);
                this.ˋ.ˋ.setVisibility(8);
            }
        });
    }

    private void ॱ(int i) {
        if (i > 0) {
            CharSequence ˋ = MinimumFactory.double2String((double) i);
            this.ˏ.setVisibility(0);
            this.ˏ.setText(ˋ);
            return;
        }
        this.ˏ.setVisibility(8);
    }

    private void ˊ(@Nullable List<ApkUpgradeInfo> list) {
        int i = 1;
        int i2 = 0;
        if (list != null && this.ᐝ.size() == list.size() && this.ᐝ.containsAll(list)) {
            i = 0;
        }
        if (i != 0) {
            if (list != null) {
                this.ᐝ = list;
            }
            this.ˏॱ.clear();
            this.ॱॱ.setVisibility(this.ᐝ.size() > 3 ? 0 : 8);
            View view = this.ˋ;
            if (this.ᐝ.size() <= 3) {
                i2 = 8;
            }
            view.setVisibility(i2);
            List arrayList = new ArrayList(3);
            arrayList.add(this.ʽ);
            arrayList.add(this.ʼ);
            arrayList.add(this.ʻ);
            ˎ(arrayList);
        }
    }

    private void ˎ(List<View> list) {
        int size = this.ᐝ.size();
        int i = 2;
        int i2 = 0;
        while (i >= 0) {
            int i3;
            View view = (View) list.get(i);
            if (i < size) {
                boolean z;
                view.setVisibility(0);
                ApkUpgradeInfo apkUpgradeInfo = (ApkUpgradeInfo) this.ᐝ.get(i);
                if (i2 != 0) {
                    z = true;
                    i3 = 0;
                } else if (i == 2) {
                    z = true;
                    i3 = 0;
                } else {
                    i3 = i2;
                    z = false;
                }
                this.ˏॱ.add(ॱ(apkUpgradeInfo, view, z));
            } else {
                view.setVisibility(8);
                i3 = 1;
            }
            i--;
            i2 = i3;
        }
    }

    private UpdateRecordCard ॱ(ApkUpgradeInfo apkUpgradeInfo, View view, boolean z) {
        view.setVisibility(0);
        UpdateRecordCard updateRecordCard = new UpdateRecordCard(getContext());
        updateRecordCard.ˏ(view);
        CardBean ˋ = dba.ˋ(apkUpgradeInfo, true);
        ˋ.ॱ(apkUpgradeInfo.ᐝ().equals(this.ॱˊ));
        ˋ.ˎ(z);
        updateRecordCard.ˏ(ˋ);
        return updateRecordCard;
    }

    public void ॱ(final boolean z) {
        this.ˎ.post(new Runnable(this) {
            final /* synthetic */ UpdateView ˋ;

            public void run() {
                this.ˋ.ॱ.setVisibility(z ? 0 : 8);
            }
        });
    }

    public void ˋ(String str) {
        ˏ(str, true);
    }

    public String ˊ() {
        return this.ॱˊ;
    }
}
